package com.deercrowing.takeaway.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.deercrowing.takeaway.vo.GoodsSalesVO;
import com.deercrowing.takeaway.vo.OrdersPageVO;
import com.deercrowing.takeaway.vo.OrdersStatusNumberVO;
import com.deercrowing.takeaway.vo.OrdersVO;
import org.apache.ibatis.annotations.Mapper;
import com.deercrowing.takeaway.entity.OrdersEntity;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * 订单表(Orders)表数据库访问层
 *
 * @author guoyongxiang
 * @since 2024-07-11 16:04:43
 */
@Mapper
public interface OrdersMapper extends BaseMapper<OrdersEntity> {

    /**
     * 根据搜索条件搜索数据
     *
     * @param ordersPageVO
     * @return
     */
    List<OrdersEntity> selectListByCondition(OrdersPageVO ordersPageVO);

    /**
     * 统计固定时段订单总和
     *
     * @param dataMap
     * @return
     */
    Integer countByMap(Map<String,Object> dataMap);

    /**
     * 统计固定时段订单销量
     *
     * @param dataMap
     * @return
     */
    Double sumByMap(Map<String,Object> dataMap);

    /**
     * 统计指定时间区间内的销量排名前10
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesVO> getSalesTop10(LocalDateTime begin, LocalDateTime end);

    /**
     * 统计各状态订单数
     *
     * @return
     */
    OrdersStatusNumberVO selectByStatusNumber();

    /**
     * 订单确认
     *
     * @param ordersVO
     */
    @Update("update tb_orders set status = #{status} where id = #{id}")
    void updateByConfirm(OrdersVO ordersVO);

    /**
     * 搜索超时订单
     *
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from tb_orders where status = #{status} and order_time < #{orderTime}")
    List<OrdersEntity> selectByStatusAndTime(Short status, LocalDateTime orderTime);

    /**
     * 定时任务，超时取消订单
     *
     * @param ordersEntity
     */
    void updateByTask(OrdersEntity ordersEntity);

    /**
     * 根据用户ID搜索订单
     *
     * @param ordersPageVO
     * @return
     */
    List<OrdersEntity> selectListByUserId(OrdersPageVO ordersPageVO);

    /**
     * 根据订单号、用户ID搜索订单
     *
     * @param orderNumber
     * @param userId
     * @return
     */
    @Select("select * from tb_orders where number = #{orderNumber} and user_id = #{userId}")
    OrdersEntity selectByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 根据条件统计订单数量
     *
     * @param ordersPageVO
     * @return
     */
    Integer countOrderNum(OrdersPageVO ordersPageVO);

    /**
     * 统计菜品\套餐月销量
     *
     * @param dataMap
     * @return
     */
    Integer sumByDishSales(Map<String,Object> dataMap);
}

